#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/*&h{*/
#ifndef FALSE
#include "\mylib\types.h"
#endif

#ifndef Max

#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
#define Round(x) ((long) ((x)+0.5))
#define EmptyStr(s) ((s)[0] == '\0')
/*&h}*/


/*&hf*/
char *CatFileName(char *s1, char *s2)
	{
	static char res[300];
	static char hilf[300];

	if (s1 == res)
		{
		strcat(res, s2);
		return(res);
		}
	else if (s1 >= res && s1 < res + 300)
		{
		strcpy(hilf, s1);
		s1 = hilf;
		}
	else if (s2 >= res && s2 < res + 300)
		{
		strcpy(hilf, s2);
		s2 = hilf;
		}
	strcpy(res, s1);
	strcat(res, s2);
	return(res);
	}


/*&hf*/
char *FileNameExt(char *s)
	{
	char *punkt, *strich;

	if ((punkt = strrchr(s, '.')) == NULL)
		return (NULL);
	else if ((strich = strrchr(s, '\\')) == NULL)
		return (punkt);
	else
		return ((punkt > strich) ? punkt : NULL);
	}


/*&hf*/
void CenterWrite(char *s, int line)
	{
	_settextposition(line, (80 - strlen(s)) / 2);
	_outtext(s);
	}


static char *SkipDot(char *p)
	{
	while (*p != '\0')
		if (*p == '.')
			return (p+1);
		else
			p++;
	return (p);
	}


/*&hf*/
bool CmpFileNameMask(char *np, char *mp)
	{
	while (TRUE)
		switch (*mp)
			{
			case '\0' :
			case '\n' :
				return (*np == '\0');
			case '\t' :
			case ' ' :
				mp++;
				break;
			case '*' :
				np = SkipDot(np);
				mp = SkipDot(mp);
				break;
			case '?' :
				if (*np == '\0')
					return (FALSE);
				mp++;
				np++;
				break;
			default :
				if (toupper(*np) != toupper(*mp))
					return (FALSE);
				mp++;
				np++;
				break;
			}
	}


/*&hf*/
void _outchar(char c)
	{
	char Buf[2];

	Buf[0] = c;
	Buf[1] = '\0';
	_outtext(Buf);
	}


/*&hf*/
void _outint(int i)
	{
	char Buf[12];

	sprintf(Buf, "%d", i);
	_outtext(Buf);
	}


/*&hf*/
void PaintLine(int col, int line)
	{
	static char Buf[81] = "";

	if (strlen(Buf) != 80)
		sprintf(Buf, "%*c", 80, ' ');
	_setbkcolor((long) col);
	_settextposition(line, 1);
	_outtext(Buf);
	}


/*&hf*/
void readln(FILE *fp)
	{
	int c;

	while ((c = getc(fp)) != EOF && c != '\n')
		;
	}


/*&hf*/
int fwrites(char *p, FILE *fp)
	{
	do
		{
		if (putc(*p, fp) == EOF)
			return(0);
		} while (*p++ != '\0');
	return(1);
	}


/*&hf*/
int fputsnl(char *p, FILE *fp)
	{
	while (*p != '\0')
		if (putc(*p++, fp) == EOF)
			return(EOF);
	if (putc('\n', fp) == EOF)
		return(EOF);
	return(0);
	}


/*&hf*/
int freads(char *p, int l, FILE *fp)
/* funktioniert mit fputsnl und fwrites */
	{
	int c;

	for ( ; l > 1 ; l--)
		if ((c = getc(fp)) == EOF || c == '\n' || c == '\0')
			break;
		else
			*p++ = c;
	*p = '\0';
	if (l <= 1)
		while ((c = getc(fp)) != EOF && c != '\n' && c != '\0')
			;
	return(ferror(fp) ? 0 : 1);
	}


/*&hf*/
char *strend(char *s)
	{
	while (*s != '\0')
		s++;
	return(s);
	}


/*&hf*/
char *strcatc(char *s, char c)
	{
	char *p;

	p = s;
	while (*p != '\0')
		p++;
	*p = c;
	p++;
	*p = '\0';
	return(s);
	}


/*&h*/
#endif /* Max */

